package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.system.domain.Country;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Keshi;
import com.ruoyi.system.service.IKeshiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2021-10-28
 */
@Controller
@RequestMapping("/system/keshi")
public class KeshiController extends BaseController
{
    private String prefix = "system/keshi";

    @Autowired
    private IKeshiService keshiService;

    @RequiresPermissions("system:keshi:view")
    @GetMapping()
    public String keshi()
    {
        return prefix + "/keshi";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:keshi:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Keshi keshi)
    {
        startPage();
        List<Keshi> list = keshiService.selectKeshiList(keshi);
        return getDataTable(list);
    }




    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:keshi:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Keshi keshi)
    {
        List<Keshi> list = keshiService.selectKeshiList(keshi);
        ExcelUtil<Keshi> util = new ExcelUtil<Keshi>(Keshi.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:keshi:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Keshi keshi)
    {
        return toAjax(keshiService.insertKeshi(keshi));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{keshiid}")
    public String edit(@PathVariable("keshiid") String keshiid, ModelMap mmap)
    {
        Keshi keshi = keshiService.selectKeshiById(keshiid);
        mmap.put("keshi", keshi);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:keshi:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Keshi keshi)
    {
        return toAjax(keshiService.updateKeshi(keshi));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:keshi:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(keshiService.deleteKeshiByIds(ids));
    }

    @PostMapping("/selectbyshangjiid")
    @ResponseBody
    public List<Map<String,Object>> selectByShangjiId(String keshiid){
        List<Map<String,Object>> mmList  = keshiService.selectByShangjiId(keshiid);
        return mmList;
    }

    @PostMapping("/jsonlist")
    @ResponseBody
    public TableDataInfo jsonlist()
    {
        startPage();
        List<Map<String,Object>> mmList = new ArrayList<>();
        List<Map<String,Object>> mmKeshiList = keshiService.selectByShangjiId("0");
        for (int i=0;i<mmKeshiList.size();i++){
            Map<String,Object> mmMap = new HashMap<>();
            mmMap.put("Code",mmKeshiList.get(i).get("keshiid").toString());
            mmMap.put("Name",mmKeshiList.get(i).get("name").toString());
            mmMap.put("level",geterji(mmKeshiList.get(i).get("keshiid").toString()));

            mmList.add(mmMap);
        }

        return getDataTable(mmList);
    }


    public  List<Map<String,Object>>  geterji(String mmId){
        List<Map<String,Object>> mmKeshiList = keshiService.selectByShangjiId(mmId);
        List<Map<String,Object>> mmList = new ArrayList<>();
        for (int i=0;i<mmKeshiList.size();i++){
            Map<String,Object> mmMap = new HashMap<>();

            mmMap.put("Code",mmKeshiList.get(i).get("keshiid").toString());
            mmMap.put("Name",mmKeshiList.get(i).get("name").toString());
            mmMap.put("sort",1);


            mmList.add(mmMap);
        }
        return mmList;
    }
}
